/**
 * @File AssetDaoImpl.java
 * @author NUPUR
 * @Version 1.0
 * @Date April 11, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.generalLedger.assetRegister;

import in.ac.dei.edrp.generalLedger.utilities.CommonFunctions;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class AssetDaoImpl extends SqlMapClientDaoSupport implements AssetDao {
	
	private static Logger logger = Logger.getLogger(AssetDaoImpl.class);
	
	/** Creating object of TransactionTemplate for transaction Management */
	TransactionTemplate transactionTemplate = null;
	
	/** defining setter method for object of TransactionTemplate */
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getCategories() {
		List<AssetBean> assetList = null ;
		try{
			assetList = new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getCategories");					
		}
		catch(Exception e){
			logger.error("Exception inside getCategories Method of AssetDaoImpl class :",e);
		}
		return assetList;
	}
	
	public String updateAsset(AssetBean assetBean){
		String updateFlag = "";
		try {						
			int update = getSqlMapClientTemplate().update("asset.updateAsset", assetBean);	
			if(update==0){
				updateFlag = "false-No record to update";
			}
			else{
				updateFlag="true";
			}
		} catch (Exception e) {
			updateFlag="false";
			logger.error("Exception inside updateAsset Method of AssetDaoImpl class :",e);
		}		
		return updateFlag;
	}
	
	@SuppressWarnings("unchecked")
	public String setAsset(final AssetBean assetBean) {		
		String s = (String) transactionTemplate.execute(new TransactionCallback() {
			public String doInTransaction(TransactionStatus tStatus) {												
				Object savepoint = null;
				String status="false";				
				try {
					savepoint = tStatus.createSavepoint();
					CommonFunctions common = new CommonFunctions(getSqlMapClient());
					String finalCode = common.generateNumericIds(assetBean.getUniversityCode(), "ASSETCD");
					common.updateNumericSystemId(assetBean.getUniversityCode(), "ASSETCD", finalCode, "add", true);
					finalCode =  assetBean.getAssetClassCode()+assetBean.getAssetSubClassCode()+finalCode;
					status="true-"+finalCode;
					assetBean.setAssetCode(finalCode);
					getSqlMapClientTemplate().insert("asset.insertAssetMaster", assetBean);					
					}catch (Exception ex) {
						logger.error("Exception inside setAsset Method of AssetDaoImpl class :",ex);
						status = "false-"+ex.getMessage();					
						tStatus.rollbackToSavepoint(savepoint);
						return status;
					}
				return status;
			}			
		});
		return s;
	}


	public String deleteAsset(AssetBean assetBean) {
		String deleteFlag = "";
		try {
			int del = getSqlMapClientTemplate().delete("asset.deleteAsset", assetBean);			
			if(del==0){
				deleteFlag = "false-No record to delete";
			}
			else{
				deleteFlag="true";
			}
		} catch (Exception e) {
			logger.error("Exception inside deleteAsset Method of AssetDaoImpl class :",e);
		}		
		return deleteFlag;
	}


	@SuppressWarnings("unchecked")
	public List<AssetBean> getMainCategory(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList = new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getMainCategories",assetBean);	
		}
		catch(Exception e){
			logger.error("Exception inside getMainCategory Method of AssetDaoImpl class :",e);

		}
		return assetList;
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getSubCategory(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList = new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getSubCategories",assetBean);					
		}
		catch(Exception e){
			logger.error("Exception inside getSubCategory Method of AssetDaoImpl class :",e);

		}
		return assetList;	
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getAllAsset(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList= new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getAllAsset",assetBean);	
		}
		catch(Exception e){
			logger.error("Exception inside getAllAsset Method of AssetDaoImpl class :",e);

		}
		return assetList;
	}
	
	@SuppressWarnings("unchecked")
	public List<AssetBean> getLocation(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList= new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getLocation",assetBean);	
		}
		catch(Exception e){
			logger.error("Exception inside getLocation Method of AssetDaoImpl class :",e);

		}
		return assetList;
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getAssetClass(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList= new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getAssetClass",assetBean);	
		}
		catch(Exception e){
			logger.error("Exception inside getAssetClass Method of AssetDaoImpl class :",e);

		}
		return assetList;
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getAssetSubclass(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList= new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getAssetSubClass",assetBean);	
		}
		catch(Exception e){
			logger.error("Exception inside getAssetSubClass Method of AssetDaoImpl class :",e);

		}
		return assetList;
	}

	@SuppressWarnings("unchecked")
	public List<AssetBean> getAccounts(AssetBean assetBean) {
		List<AssetBean> assetList = null;
		try{
			assetList= new ArrayList<AssetBean>();
			assetList = getSqlMapClientTemplate().queryForList("asset.getGroupAccounts",assetBean);	
			List<AssetBean> childList = getSqlMapClientTemplate().queryForList("asset.getChildAccounts",assetBean);	
			for(int i=0;i<childList.size();i++){
				AssetBean bean = new AssetBean();
				bean.setAccountCode(childList.get(i).getAccountCode());
				bean.setAccountName(childList.get(i).getAccountName());
				bean.setParentAccountCode(childList.get(i).getParentAccountCode());
				bean.setChildCount(childList.get(i).getChildCount());
				bean.setAccountTypeName(childList.get(i).getAccountTypeName());
				assetList.add(bean);
			}
		}
		catch(Exception e){
			logger.error("Exception inside getAccounts Method of AssetDaoImpl class :",e);
		}
		return assetList;
	}	
	
	
	
}
